package com.djinfo.work.service.impl;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.dao.QsQuestionDao;
import com.djinfo.work.dao.QsTitleDao;
import com.djinfo.work.entity.QsQuestionEntity;
import com.djinfo.work.entity.QsTitleEntity;
import com.djinfo.work.service.QsTitleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;





@Service("qsTitleService")
public class QsTitleServiceImpl implements QsTitleService {
	@Autowired
	private QsTitleDao qsTitleDao;
	@Autowired
	private QsQuestionDao questionDao;
	@Override
	public AjaxJson saveOrUpdate(QsTitleEntity qsTitle, HttpServletRequest request) {
		String userId=(String)request.getSession().getAttribute("userId");
		AjaxJson ajaxJson=new AjaxJson();
		String dateStr= StringUtil.getNowTime();
		if("".equals(qsTitle.getId())||qsTitle.getId()==null){
			try {
				qsTitle.setId(null);
				qsTitle.setCreateTime(dateStr);
				qsTitle.setUpdateTime(dateStr);
				qsTitle.setCreateUser(userId);
				qsTitle.setUpdateUser(userId);
				qsTitleDao.save(qsTitle);
				ajaxJson.setCode(101);
			} catch (Exception e) {
				ajaxJson.setCode(201);
				e.printStackTrace();
			}
		}else {
			try {
				qsTitle.setUpdateTime(dateStr);
				qsTitleDao.update(qsTitle);
				ajaxJson.setCode(101);
			} catch (Exception e) {
				ajaxJson.setCode(201);
				e.printStackTrace();
			}
		}
		return ajaxJson;
	}

	@Override
	public AjaxJson delete(QsTitleEntity qsTitle) {
		AjaxJson ajaxJson=new AjaxJson();
		try {
			qsTitleDao.delete(qsTitle);
			ajaxJson.setCode(101);
		} catch (Exception e) {
			ajaxJson.setCode(201);
			e.printStackTrace();
		}
		return ajaxJson;
	}



	@Override
	public AjaxJson getList(QsTitleEntity qsTitleEntity, Integer pagenum, Integer pagesize) {
		AjaxJson ajaxJson=new AjaxJson();
		HashMap map=new HashMap();
		int count=qsTitleDao.getAllRowCount(qsTitleEntity);
		List<QsTitleEntity> list=qsTitleDao.page(qsTitleEntity,pagenum,pagesize);
		map.put("dataList",list);
		map.put("total",count);
		ajaxJson.setResult(map);
		return ajaxJson;
	}

	@Override
	public QsTitleEntity getQsTitleById(String id) {
		return qsTitleDao.selectOne(id);
	}

	@Override
	public AjaxJson getTitleListBySearch(QsTitleEntity qsTitleEntity) {
		AjaxJson ajaxJson=new AjaxJson();
		HashMap map=new HashMap();
		List<QsTitleEntity> list=qsTitleDao.getTileListBySearch(qsTitleEntity);
		QsTitleEntity qsTitleEntity1=new QsTitleEntity();
		qsTitleEntity.setTitleName("设为一级标题");
		qsTitleEntity.setId("0");
		list.add(0,qsTitleEntity);
		map.put("titleList",list);
		ajaxJson.setResult(map);
		return ajaxJson;
	}

	@Override
	public AjaxJson getQuestionList(QsQuestionEntity qsQuestionEntity) {
		AjaxJson ajaxJson=new AjaxJson();
		HashMap map=new HashMap();
		List list=questionDao. list(qsQuestionEntity);
		map.put("questionList",list);
		ajaxJson.setResult(map);
		return ajaxJson;
	}
	/*获取该问卷ID下的所有title*/
	@Override
	public List getTitleListById(String id) {
		String hql= " from QsTitleEntity where parentId = ? order by order ";

		return (List<QsQuestionEntity>)qsTitleDao.getTitleListById(hql,id);
	}
	/*获取title最大level*/
	public Integer getTLevelMaxByQsId(String id){
		String hql= "SELECT MAX(level) FROM QsTitleEntity WHERE questionId = ? ";
		return qsTitleDao.getTLevelMaxByQsId(hql,id);

	}


	@Override
	public List<QsTitleEntity> getTitleListByLevel(String qid, String level) {
		String hql= " from QsTitleEntity where questionId = ? and level = ? order by order";
		List list=qsTitleDao.getTitleListByQsIdAndLevel(hql,qid,level);
		return list;
	}

}